package com.manager.modelserver.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.manager.modelserver.entry.PhSubmitOrderDetailQueryItem;
import com.manager.modelserver.entry.SubTotal;
import com.manager.modelserver.service.SubTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SubTotalServiceImpl implements SubTotalService {
    @Autowired
    private Gson gson;

    @Override
    public String subTotal(String rows1) {
        Map<String, SubTotal> lmap = new LinkedHashMap<>();
        Set<String> qty = new HashSet<>(); //用于存放不重复的系统箱号
        List<PhSubmitOrderDetailQueryItem> rows = gson.fromJson(rows1, new TypeToken<List<PhSubmitOrderDetailQueryItem>>() {
        }.getType());
        for (PhSubmitOrderDetailQueryItem row : rows) {
            String str = row.getBatchNo() + "," + row.getCurrencyDesc();
            if (lmap.containsKey(str)) {
                SubTotal a = lmap.get(str);
                a.setSeqNo(row.getSeqNo()); //序号
                a.setBatchNo(row.getBatchNo());//Y单号
                a.setCurrencyDesc(row.getCurrencyDesc());//发票
                if (a.getGrossWeight() == null || a.getGrossWeight().equals("")) {
                    a.setGrossWeight(0.0);
                }
                a.setGrossWeight(a.getGrossWeight() + row.getGrossWeight());//重量

                if (a.getVolume() == null || a.getVolume().equals("")) {
                    a.setVolume(0.0);
                }
                a.setVolume(a.getVolume() + row.getVolume());//体积

                if (a.getOutQty() == null || a.getOutQty().equals("")) {
                    a.setOutQty(0.0);
                }
                a.setOutQty(a.getOutQty() + row.getOutQty());//可出数量

                a.setIsShot("0");

                String it[] = row.getExtItemCode().split("#");

                a.setItCode(it[1]);

                if (qty.add(row.getPackageNo())) {  //判断之前是否有过此系统箱号,没有则可以计算箱数
                    a.setPackageNo(row.getPackageNo());  //系统箱号
                    if (a.getSecondQty() == null || "".equals(a.getSecondQty())) {
                        a.setSecondQty(row.getSecondQty());
                    }else{
                        a.setSecondQty(a.getSecondQty()+row.getSecondQty());
                    }
                } else {
                    if (a.getSecondQty() == null || "".equals(a.getSecondQty())){
                        a.setSecondQty(0.0);
                    }
                    a.setPackageNo(row.getPackageNo());  //系统箱号

                }
                lmap.put(str, a);

            } else {

                SubTotal a = new SubTotal();
                a.setSeqNo(row.getSeqNo()); //序号
                a.setBatchNo(row.getBatchNo());//Y单号
                a.setCurrencyDesc(row.getCurrencyDesc());//发票
                if (a.getGrossWeight() == null || a.getGrossWeight().equals("")) {
                    a.setGrossWeight(0.0);
                }
                a.setGrossWeight(a.getGrossWeight() + row.getGrossWeight());//重量

                if (a.getVolume() == null || a.getVolume().equals("")) {
                    a.setVolume(0.0);
                }
                a.setVolume(a.getVolume() + row.getVolume());//体积

                if (a.getOutQty() == null || a.getOutQty().equals("")) {
                    a.setOutQty(0.0);
                }
                a.setOutQty(a.getOutQty() + row.getOutQty());//可出数量
                a.setIsShot("0");

                String it[] = row.getExtItemCode().split("#");

                a.setItCode(it[1]);
                if (qty.add(row.getPackageNo())) {
                    a.setPackageNo(row.getPackageNo());  //系统箱号
                    if (a.getSecondQty() == null || "".equals(a.getSecondQty())) {
                        a.setSecondQty(row.getSecondQty());
                    }else{
                        a.setSecondQty(a.getSecondQty()+row.getSecondQty());
                    }
                } else {
                    if (a.getSecondQty() == null || "".equals(a.getSecondQty())){
                        a.setSecondQty(0.0);
                    }
                    a.setPackageNo(row.getPackageNo());  //系统箱号

                }
                lmap.put(str, a);
            }

        }

        ArrayList<SubTotal> array = new ArrayList<SubTotal>();
        Set<Map.Entry<String, SubTotal>> entries = lmap.entrySet();
        for (Map.Entry entry : entries) {
//            System.out.println("key:" + entry.getKey() + " "
//                    + "value:" + entry.getValue());
            array.add((SubTotal) entry.getValue());
        }
//        System.out.println(array);
//        for (SubTotal s : array
//        ) {
//            System.out.println(s);
//        }
        String json = gson.toJson(array);
        return json;
    }
}
